package com.dongdongshop.data;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * Created by IntelliJ IDEA.
 */
public class ESPageUtil {

    public static Long getTotal(JsonObject jsonObject) {
        String[] split = "hits/total".split("/");
        Long total = null;
        JsonObject obj = getPath(split, jsonObject);
//        if (obj != null) total = obj.getAsLong();
//        JsonElement value = obj.get("value");
        if (obj != null) total = obj.get("value").getAsLong();
        return total;
    }

    public static JsonObject getPath(String[] path, JsonObject jsonObject) {
        JsonObject retval = null;
        if (jsonObject != null) {
            JsonObject obj = jsonObject;
            for (String component : path) {
                if (obj == null) break;
                JsonElement jsonElement = ((JsonObject) obj).get(component);
                obj = (JsonObject) jsonElement;
            }
            retval = obj;
        }
        return retval;
    }
}
